import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Home from '../components/home'
import Welcome from '../components/Welcome'
import User from '../components/User'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Cate from "../components/goods/Cate"

import '../assets/css/common.css'
import '../assets/fonts/iconfont.css'



Vue.use(Router);

const route= new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect:'/welcome',
      children:[
        {
          path:'/welcome',
          component:Welcome
        },
        {
          path:'/rights',
          component:Rights
        },
        {
          path:'/roles',
          component:Roles
        },
        {
          path:'/users',
          component:User
        },
        {
          path:'/categories',
          name:'Cate',
          component:Cate,
        },
      ]
    },

    {
      path: '/',
      redirect:'/login'
    }
  ],
  mode:"history",
});

//挂载路由导航守卫
route.beforeEach((to,from,next)=>{
  //to表示将要跳去的地方  from表示从哪儿跳过来的  next表示放行  next('路径')表示强制跳转
  if(to.path=='/login'){
    return next()
  }else{
    const tokenStr=window.sessionStorage.getItem('token');
    if(!tokenStr){
      return next('/login');
    }else{
      next();
    }
  }
});
export default route;
